package exam.wzb.jsp.controller;

import exam.wzb.jsp.dao.impl.AccountDaoImpl;
import exam.wzb.jsp.service.AccountService;
import exam.wzb.jsp.service.impl.AccountServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

@WebServlet("/account")
public class AccountController extends HttpServlet {

    private AccountService accountService; // 这里通常需要使用Spring的依赖注入特性，但Servlet原生不支持，可以使用Spring提供的工具类来获取bean。



    // 处理转账的请求
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int fromAccountId = Integer.parseInt(request.getParameter("fromAccountId"));
        int toAccountId1 = Integer.parseInt(request.getParameter("toAccountId1"));
        int toAccountId2 = Integer.parseInt(request.getParameter("toAccountId2"));
        BigDecimal amount = new BigDecimal(request.getParameter("amount"));

        try {
            accountService.transfer(fromAccountId, toAccountId1, toAccountId2, amount);
            response.getWriter().write("Transfer successful!");
        } catch (Exception e) {
            response.getWriter().write("Transfer failed: " + e.getMessage());
        }
    }
}